param (
    [string]$user="",
    [string]$user_pwd="",
    [string]$server_name
)

$DATA_BACKUP_AGENT_HOME = (Get-ChildItem env:DATA_BACKUP_AGENT_HOME).Value
$Location = $DATA_BACKUP_AGENT_HOME + "\DataBackup\ProtectClient\Plugins\GeneralDBPlugin\bin\applications\exchange\ps1_scripts"
Set-Location -Path $Location
[string]$common_file_path = (Get-Location).Path + "\Common.ps1"
[string]$pssession_name = "GetServerGuid"

# 引用Common.ps1
. $common_file_path

# 建立连接
if (!(NewConnection $User $User_pwd $pssession_name)) {
    return $False
}

# 如果server_name为空，使用localhost
if ([string]::IsNullOrEmpty($server_name)) {
    [string]$server_name = hostname
}

$server = Invoke-Command -Session $Session -ScriptBlock {Get-ExchangeServer -identity $using:server_name}
if (!$?) {
    ClosePSSession $pssession_name
    return $False
}
$server_guid = [string]$server.Guid

ClosePSSession $pssession_name
return $server_guid